<template>
  <el-dialog
    title="分配角色"
    width="700px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <!-- checkbox组 
el-checkbox-group
el-checkbox:  label选中该项后的值
-->

    <h3>{{ roleIds }}</h3>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox :label="item.id" v-for="item in list" :key="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <div>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRolesPut } from '@/api/employees'
export default {
  data () {
    return {
      isShow: false,
      roleIds: [], // 当前用户的角色数据
      list: [], // 角色列表
      id: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysRole()
      // 存储角色列表
      this.list = res.data.rows
      console.log('角色列表', res)
    },
    // 定义方法用于获取当前用户的角色信息并回显
    async getUserRoles (id) {
      this.id = id
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds
      console.log('当前用户的角色信息', res)
    },
    // 确定点击
    async submit () {
      await sysUserAssignRolesPut({
        id: this.id,
        roleIds: this.roleIds
      })
      this.$message.success('修改角色成功')
      //   关闭弹框
      this.isShow = false
    },
    closeEvent () {
      // 清空已勾选数据
      this.roleIds = []
    }
  }
}
</script>
<style></style>
